/*
 * Puppeteer 简单爬虫
 */

const puppeteer = require('puppeteer');

const timestart = new Date().getTime();
// 启动 chrome
(async() => {
	// 默认启动内嵌的 chrome
	const browser = await puppeteer.launch(
		{args: ['--no-sandbox']}
	);
	// open webpage
	const page = await browser.newPage();
	await page.setRequestInterception(true);
	page.on('request', interceptedRequest => {
		const url = interceptedRequest.url().toLowerCase();
		for (name of ['.jpg', '.jpeg', '.png', '.gif']){
			if (url.endsWith(name)){
				// 终止请求
				interceptedRequest.abort();
				return ;
			}
		}
		interceptedRequest.continue();
	});
	await page.goto('http://www.hao6v.com/', {waitUntil: 'domcontentloaded'});
	const listHdlArr = await page.$x('//div[@class="box" and h3/text()="今日推荐"]/ul/li');
	if (listHdlArr == null){
		console.log('找不到今日推荐列表');
		return ;
	}
	// 逐个处理 列表项
	let result = [];
	await (async()=>{
	  for (liHdl of listHdlArr){
	  //listHdlArr.forEach(async(liHdl) => {
		// img
		const imgHdl = await liHdl.$x('.//img/@src');
		const img = await textValue(imgHdl[0], page);
		// title
		const titleHdl = await liHdl.$x('.//font/text()');
		const title = await textValue(titleHdl[0], page);
		// url
		const urlHdl = await liHdl.$x('a/@href');
		const url = await textValue(urlHdl[0], page);
		
		result.push({ img, title, url })
	  }
	})();
	
	//  输出
	console.log(JSON.stringify(result, null, 4))
	// close browser
	await browser.close();
	
	const timeend = new Date().getTime();
	console.log(timestart, timeend, timeend - timestart);
})();

async function textValue(jsHandle){
	const text = await jsHandle.executionContext().evaluate(e=> e.textContent, jsHandle);
	return text;
}
